package com.ptmall.app.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.TitleBarView;

import java.util.HashMap;
import java.util.Map;

//import cn.smssdk.EventHandler;
//import cn.smssdk.SMSSDK;

public class RegisterPassActivity extends BaseMvpActivity implements View.OnClickListener {

    TitleBarView titlebar;
    TextView getcode;
    EditText phone_edit,smscode,pass1,pass2;
    Button enter;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

//    EventHandler eventHandler = new EventHandler() {
//        public void afterEvent(int event, int result, Object data) {
//            // afterEvent会在子线程被调用，因此如果后续有UI相关操作，需要将数据发送到UI线程
//            Message msg = new Message();
//            msg.arg1 = event;
//            msg.arg2 = result;
//            msg.obj = data;
//            new Handler(Looper.getMainLooper(), new Handler.Callback() {
//                @Override
//                public boolean handleMessage(Message msg) {
//                    int event = msg.arg1;
//                    int result = msg.arg2;
//                    Object data = msg.obj;
//                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//                        if (result == SMSSDK.RESULT_COMPLETE) {
//                            // TODO 处理成功得到验证码的结果
//                            // 请注意，此时只是完成了发送验证码的请求，验证码短信还需要几秒钟之后才送达
//
//                            showMsg("验证码发送成功");
//                        } else {
//                            // TODO 处理错误的结果
//                            ((Throwable) data).printStackTrace();
//                            mCountDownTimer.cancel();
//                            getcode.setEnabled(true);
//                            getcode.setText("发送失败");
//                        }
//                    } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//                        if (result == SMSSDK.RESULT_COMPLETE) {
//                            // TODO 处理验证码验证通过的结果
//                            resetPass();
//                        } else {
//                            // TODO 处理错误的结果
//                            ((Throwable) data).printStackTrace();
//                            showMsg("短信验证码错误");
//                        }
//                    }
//                    // TODO 其他接口的返回结果也类似，根据event判断当前数据属于哪个接口
//                    return false;
//                }
//            }).sendMessage(msg);
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_registerpass);
    }

    @Override
    public void initViews() {
        enter = (Button) findViewById(R.id.enter);
        pass1 = (EditText) findViewById(R.id.pass1);
        pass2 = (EditText) findViewById(R.id.pass2);
        smscode = (EditText) findViewById(R.id.smscode);
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        getcode = (TextView) findViewById(R.id.getcode);
        phone_edit = (EditText) findViewById(R.id.phone_edit);
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

    }

    @Override
    public void initEvents() {
        getcode.setOnClickListener(this);
        enter.setOnClickListener(this);
//        SMSSDK.registerEventHandler(eventHandler);
    }


    private CountDownTimer mCountDownTimer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            getcode.setText((millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            getcode.setEnabled(true);
            getcode.setText("获取验证码");
        }
    };

    private void resetPass(){
        Map<String,String> map = new HashMap<>();
        map.put("tel",phone_edit.getText().toString());
        map.put("password",pass1.getText().toString());
        apiDataRepository.resetPass(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {
                showMsg("重置密码成功");
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.getcode:
                if(phone_edit.getText().toString().equals("")){
                    showMsg("请输入手机号");
                    return;
                }

//                SMSSDK.getVerificationCode("86", phone_edit.getText().toString());

                mCountDownTimer.start();
                getcode.setEnabled(false);
                break;
            case R.id.enter:
                if(phone_edit.getText().toString().equals("")){
                    showMsg("请输入手机号");
                    return;
                }
                if(smscode.getText().toString().equals("")){
                    showMsg("请输入验证码");
                    return;
                }
                if(pass1.getText().toString().equals("")){
                    showMsg("请输入密码");

                    return;
                }
                if(!pass1.getText().toString().equals(pass2.getText().toString())){
                    showMsg("两次密码不一致");

                    return;
                }
//                SMSSDK.submitVerificationCode("86", phone_edit.getText().toString(),smscode.getText().toString() );

                break;
        }
    }
}
